let wxpay = require('../../utils/pay.js')
let app = getApp()

Page({
	data: {
    balance:0,
    freeze:0,
    score:0,
    score_sign_continuous:0,
    level: 2,
    logo: '/images/default-head.png',
    info: {},
    prizeCount: 0,
    couponCount: 0,
    score: 0,
    money: 0,
    toPay: 0,
    toSend: 0,
    toReceive: 0,
    toComment: 0,
    toAfterSale: 0
  },
	onLoad() {
    this.onShow()
	},	
  onShow() {
    this.getUserInfo()
    this.getMyInfo()
  },	
  getUserInfo: function (cb) {
      var that = this
      wx.login({
        success: function () {
          wx.getUserInfo({
            success: function (res) {
              that.setData({
                userInfo: res.userInfo,
                logo: res.userInfo.avatarUrl
              });
            }
          })
        }
      })
  },
  toMyOrderList: function (e) {
    wx.navigateTo({
      url: "/pages/order-list/index?type=0"
    })
  },
  toWaitfk: function () {
    wx.navigateTo({
      url: "/pages/order-list/index?type=1"
    })
  },
  toWaitfh: function () {
    wx.navigateTo({
      url: "/pages/order-list/index?type=2"
    })
  },
  toWaitsh: function () {
    wx.navigateTo({
      url: "/pages/order-list/index?type=3"
    })
  },

  toWaitpj: function () {
    wx.navigateTo({
      url: "/pages/order-list/index?type=4"
    })
  },
  toFocusList: function () {
    wx.navigateTo({
      url: "/pages/focus-list/index"
    })
  },
  toAddressList: function (e) {
    wx.navigateTo({
      url: "/pages/select-address/index?fromID=0"
    })
  },
  toMyCoupons: function (e) {
    wx.navigateTo({
      url: "/pages/my-coupon-list/index"
    })
  },

  toMyAwards: function (e) {
    wx.navigateTo({
      url: "/pages/my-award-list/index"
    })
  },
  toCollectList: function (e) {
    wx.navigateTo({
      url: "/pages/collected-list/index"
    })
  },
  toLottery: function (e) {
    wx.navigateTo({
      url: "/pages/lottery/index"
    })
  },
  toBalance: function (e) {
    wx.navigateTo({
      url: "/pages/balance/index"
    })
  },
  
  recharge: function () {
    wx.navigateTo({
      url: "/pages/recharge/index"
    })
  },
  withdraw: function () {
    wx.navigateTo({
      url: "/pages/withdraw/index"
    })
  },
  toCustomerService: function () {
    wx.navigateTo({
      url: "/pages/service-list/index?type=1"
    })
  },

  /**
   * 获取头像失败 
   */
  errorFunction: function () {
    this.setData({
      logo: '/images/default-head.png'
    })
  },

  /**
  * 获取我的信息
  */
  getMyInfo: function () {
    let token = wx.getStorageSync('token')
    let that = this
    console.log('getmyinfo')
    wx.request({
      url: app.apiUrl('/user/index'),
      method: 'GET',
      header: {
        "Authorization": 'Bearer ' + token
      },
      data: {
       
      },
      success: function (res) {
        console.log(res.data)
        if (res.data.code > app.globalData.successCode) {
          wx.showToast({
            title: res.data.error_message ? res.data.error_message : app.globalData.unknownError,
            icon: 'none',
            duration: 2000
          })
        } else if (res.data.code == app.globalData.successCode) {
          app.login(that, 0)
        } else {
          that.getPrizeCount()
          that.getCouponCount()
          that.setData({
            info: res.data.data,
            score: res.data.data.user.score,
            money: res.data.data.user.money.toFixed(2),
            toPay: res.data.data.to_pay,
            toSend: res.data.data.to_send,
            toReceive: res.data.data.to_recieve,
            toComment: res.data.data.finished,
            toAfterSale: res.data.data.to_aftersale
          })
        }

      },
      fail: function (res) {
        console.log(res)
      },
      complete: function (res) {
        // wx.hideToast()
        //console.log(res)
      }
    })
  },

  /**
  * 获取奖品数量
  */
  getPrizeCount: function () {
    let token = wx.getStorageSync('token')
    let that = this
    console.log('getprizecount')
    wx.request({
      url: app.apiUrl('/market/prize/count'),
      method: 'GET',
      header: {
        "Authorization": 'Bearer ' + token
      },
      data: {

      },
      success: function (res) {
        console.log(res.data)
        if (res.data.code > app.globalData.successCode) {
          wx.showToast({
            title: res.data.error_message ? res.data.error_message : app.globalData.unknownError,
            icon: 'none',
            duration: 2000
          })
        } else if (res.data.code == app.globalData.successCode) {
          app.login(that, 0)
        } else {
          that.setData({
            prizeCount: res.data.data.count
          })
        }

      },
      fail: function (res) {
        console.log(res)
      },
      complete: function (res) {
        // wx.hideToast()
        //console.log(res)
      }
    })
  },

  /**
  * 获取优惠券数量
  */
  getCouponCount: function () {
    let token = wx.getStorageSync('token')
    let that = this
    console.log('getcouponcount')
    wx.request({
      url: app.apiUrl('/market/coupon/countvalid'),
      method: 'GET',
      header: {
        "Authorization": 'Bearer ' + token
      },
      data: {

      },
      success: function (res) {
        console.log(res.data)
        if (res.data.code > app.globalData.successCode) {
          wx.showToast({
            title: res.data.error_message ? res.data.error_message : app.globalData.unknownError,
            icon: 'none',
            duration: 2000
          })
        } else if (res.data.code == app.globalData.successCode) {
          app.login(that, 0)
        } else {
          that.setData({
            couponCount: res.data.data.count
          })
        }

      },
      fail: function (res) {
        console.log(res)
      },
      complete: function (res) {
        // wx.hideToast()
        //console.log(res)
      }
    })
  }

})